--File ： ChapterView.lua
--Author : zee
--Date   : 2015/4/8
--Description : 主界面，从scene来，用于组织所有的layer，层级等等。

require "ui/BaseView"


require "ui/gameStart/StartLayer"
require "ui/gameStart/RankNode"
require "ui/gameStart/RankItem"


require "ui/gamePlay/PlayLayer"
require "ui/gamePlay/PlayRuleNode"
require "ui/gamePlay/PlayEndNode"
require "ui/gamePlay/PlayCub"
require "ui/gamePlay/PlayGameData"
require "ui/gamePlay/PlayRankData"




UI = class("UI",function()
    -- 继承
    local ret = cc.Layer:create()
    
    ret.layer_view = nil
    ret.layer_box = nil
    ret.layer_info = nil

    ret.ary_view = {}
    ret.ary_box = {}

    return ret
end)


UI.__index = UI
UI.__instance = nil


function UI:create()
    local ret = UI:new()
    ret:init()
    return ret
end


function UI:getInstance()
    if( UI.__instance==nil ) then 
        UI.__instance = UI:create()
    end
    return UI.__instance
end




function UI:init()
    self:preloadAllResource()

    self:setAnchorPoint(0,0)
    self.layer_view  = cc.Layer:create()
    self.layer_box = cc.Layer:create()
    self.layer_info = cc.Layer:create()

    self:addChild( self.layer_view, 1)
    self:addChild( self.layer_box, 2)
    self:addChild( self.layer_info, 3)

end



--------------------------------------------------------
-- 初始化
--------------------------------------------------------
function UI:start()
    -- main:
    local sl = StartLayer:create()
    self:pushView(sl)
    sl:start()
    
    -- 添加逐帧方法:
    local function frameEvent(dt) 
        self:onFrame(dt) 
    end
    self:scheduleUpdateWithPriorityLua( frameEvent, 0 )
end


-- 预加载所有资源
function UI:preloadAllResource()
    cc.SpriteFrameCache:getInstance():addSpriteFrames("image/common.plist");
    cc.SpriteFrameCache:getInstance():addSpriteFrames("image/friend.plist");
    cc.SpriteFrameCache:getInstance():addSpriteFrames("image/jiazai.plist");
    cc.SpriteFrameCache:getInstance():addSpriteFrames("image/plaza.plist");
    cc.SpriteFrameCache:getInstance():addSpriteFrames("image/table.plist");
end




function UI:onFrame(dt)
end







--[[------------------------------------------------------
游戏的架构分为：
    1. view :   基本的全屏界面，触摸不可穿透。
    2. box  :   基本的非全屏界面，触摸不可穿透，触摸周围有独立事件。
    3. info :   基本的提示信息，最上层出现。
--------------------------------------------------------]]


function UI:pushView( ViewObj )
    local viewCount = getTableCount( self.ary_view )
    self.layer_view:addChild( ViewObj )
    ViewObj:setLocalZOrder( viewCount + 1)
    -- 让倒数第二层的视图隐藏，节省绘制。
    if( self.ary_view[ viewCount ] ~=nil ) then
        self.ary_view[ viewCount ]:setVisible(false)
    end
    self.ary_view[ viewCount+1 ] = ViewObj
end



function UI:popView( ViewObj, bClean )
    local viewCount = getTableCount( self.ary_view )
    if viewCount==1 then 
        return nil end
    for i=viewCount, 1, -1 do 
        if( self.ary_view[i]==ViewObj )then 
            self.ary_view[i]:removeFromParent()
            self.ary_view[i] = nil
            self:rankView()
            return nil
        end
    end
end



function UI:popLastView( ViewObj, bClean )
    local viewCount = getTableCount( self.ary_view )
    if viewCount==1 then 
        return nil end
    self.ary_view[viewCount]:removeFromParent()
    self.ary_view[viewCount] = nil
    self:rankView()
end



function UI:popToViewByKey( key, bClean )
    local viewCount = getTableCount( self.ary_view )
    if viewCount==1 then 
        return nil end
    local clean = false 
    for i=1, viewCount, 1 do 
        if( self.ary_view[i]~=nil and self.ary_view[i]:getKey()==key )then 
            clean = true
        end
        if( clean==true ) then
            self.ary_view[i]:removeFromParent()
            self.ary_view[i] = nil
        end
    end
    self:rankView()
end



function UI:popToRootView( ViewObj, bClean )
    local viewCount = getTableCount( self.ary_view )
    if viewCount==1 then 
        return nil end
    local rootKey = self.ary_view[1]:getKey()
    self:popToViewByKey()
    for i=viewCount, 1, -1 do 
        if( self.ary_view[i]==ViewObj )then 
            self.ary_view[i]:removeFromParent()
            self.ary_view[i] = nil
            self:rankView()
            return nil
        end
    end
end


--------------------------------------------------------
function UI:showBox( BoxObj )

end



--------------------------------------------------------
function UI:showInfo( infoStr, colorIndex, delayTime )

end







--------------------------------------------------------
--------------------------------------------------------
function UI:rankView( maxCount )
    maxCount = maxCount or getTableCount( self.ary_view )
    local c = getTableCount( self.ary_view )
    if( maxCount<c ) then maxCount=c*2 end
    ----------- 重新排列view队列。
    local nowId = 1
    local ary = {}
    for i=1, maxCount, 1 do 
        if( self.ary_view[i]~=nil ) then 
            ary[nowId] = self.ary_view[i]
            ary[nowId]:setLocalZOrder( nowId )
            nowId = nowId + 1
        end
    end
    self.ary_view = nil
    self.ary_view = ary
    -- 让最上层的view显示
    local viewCount = getTableCount( self.ary_view )
    if( self.ary_view[ viewCount ] ~=nil ) then
        self.ary_view[ viewCount ]:setVisible(true)
    end
end










